package com.ecar.api.quartz.jobs;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.print.attribute.standard.Sides;

import net.sf.ehcache.search.expression.Or;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ecar.api.service.common.ActivityService;
import com.ecar.api.service.common.ExceptionHandleService;
import com.ecar.api.service.common.NotifyService;
import com.ecar.api.service.driver.CarService;
import com.ecar.api.service.driver.DriverService;
import com.ecar.api.service.order.OrderService;
import com.ecar.api.service.temp.BaseInfoService;
import com.ecar.api.service.temp.TempService;
import com.ecar.api.support.dto.Constants;
import com.ecar.api.support.dto.order.DriverWilddogDto;
import com.ecar.api.support.util.DateUtil;
import com.ecar.api.support.util.GaoDeMapUtil;
import com.ecar.api.support.util.OrderUtil;
import com.ecar.api.support.util.Props;
import com.ecar.api.support.util.Redis;
import com.ecar.api.support.util.TempHttpUtil;
import com.ecar.api.support.util.WilddogUtil;
import com.ecar.common.dto.DataBean;
import com.ecar.common.util.BeanUtils;
import com.ecar.common.util.DataValidateUtil;
import com.ecar.common.util.JSONUtil;
import com.ecar.common.util.StringUtil;
import com.ecar.core.common.entity.Coupon;
import com.ecar.core.common.entity.CouponActivities;
import com.ecar.core.driver.dao.CarMapper;
import com.ecar.core.driver.dao.DriverMapper;
import com.ecar.core.driver.dao.ModelSetMapper;
import com.ecar.core.driver.entity.Car;
import com.ecar.core.driver.entity.Driver;
import com.ecar.core.driver.entity.ExceptionHandle;
import com.ecar.core.driver.entity.ModelSet;
import com.ecar.core.order.dao.OrderPriceDetailMapper;
import com.ecar.core.order.entity.Order;
import com.ecar.core.order.entity.OrderPriceDetail;
import com.wilddog.client.DataSnapshot;
import com.wilddog.client.SyncError;
import com.wilddog.client.SyncReference;
import com.wilddog.client.ValueEventListener;

/**
 * 测评所需的定时任务
 * Created by fire on 2018/1/24.
 * 
 */
public class TempJob {

	private static final Logger log = LoggerFactory.getLogger(TempJob.class);

	@Autowired
	BaseInfoService baseInfoService;

	@Autowired
	TempService tempService;

	@Autowired
	ExceptionHandleService exceptionHandleService;

	@Autowired
	DriverMapper driverMapper;
	
	@Autowired
	DriverService driverService;

	@Autowired
	OrderService orderService;

	@Autowired
	CarMapper carMapper;

	@Autowired
	OrderPriceDetailMapper orderPriceDetailMapper;

	public void ratedDriverPunish() throws Exception{
		System.out.println("=========测评=========="+DateUtil.getCurDateTime());
		int i=0;
		/**
		 * 27驾驶员处罚信息
		 * @author fire
		 */
		String url="/rated/driverpunish";
		List<ExceptionHandle> list = exceptionHandleService.selectAll();
		int pk=0;
		JSONArray  array = new JSONArray();
		for(ExceptionHandle exceptionHandle:list){
			pk++;
			Driver driver=driverService.selectByPrimaryKey(exceptionHandle.getDriverId());
			JSONObject ratedDriverPunish = new JSONObject();
			String companyId=Props.getValue("ecar.companyId");
			ratedDriverPunish.put("CompanyId", companyId);
			ratedDriverPunish.put("LicenseId", driver.getLicenseId()==null?"111111":driver.getLicenseId());
			ratedDriverPunish.put("PunishTime", Long.parseLong(DateUtil.getCurDateTime(DateUtil.DATE_FORMAT_COMPACTFULL)));
			ratedDriverPunish.put("PunishResult", "记录在案");
			array.add(ratedDriverPunish);
		}
		i=TempHttpUtil.sendPost_JSON(url, array, "ratedDriverPunish");
		log.info("第27条共推送{}条数据",pk);
		System.out.println("==========27======"+i+"================");

		//url="/rated/passenger";
		List<Order> list2=orderService.selectTodayOrder();
		/**
		 * 15订单发起
		 * @author fire
		 */
		url="/order/create";
		array = new JSONArray();
		pk=0;
		for(Order order:list2){
			pk++;
			JSONObject orderCreate = new JSONObject();
			String companyId=Props.getValue("ecar.companyId");
			orderCreate.put("CompanyId", companyId);
			orderCreate.put("Address", 420107);
			orderCreate.put("OrderId", order.getOrderNum());
			orderCreate.put("DepartTime", Long.parseLong(DateUtil.parseDateToString(order.getPlanTime(), DateUtil.DATE_FORMAT_COMPACTFULL)));
			orderCreate.put("OrderTime", Long.parseLong(DateUtil.parseDateToString(order.getCreateTime(), DateUtil.DATE_FORMAT_COMPACTFULL)));
			orderCreate.put("Departure", order.getStartAddress());
			String start=order.getStartLocation();
			String [] starts=start.split(":");

			starts[0]=starts[0].concat("000000");
			starts[1]=starts[1].concat("000000");
			String lon=starts[0].substring(0, 9);
			lon=lon.replace(".", "");
			String lat=starts[1].substring(0, 10);
			lat=lat.replace(".", "");
			orderCreate.put("DepLongitude", Long.parseLong(lon));
			orderCreate.put("DepLatitude", Long.parseLong(lat));
			orderCreate.put("Destination", order.getDesAddress());

			String des=order.getDesLocation();
			String [] dess=des.split(":");

			dess[0]=dess[0].concat("000000");
			dess[1]=dess[1].concat("000000");

			lon=dess[0].substring(0, 9);
			lon=lon.replace(".", "");
			lat=dess[1].substring(0, 10);
			lat=lat.replace(".", "");
			orderCreate.put("DestLongitude", Long.parseLong(lon));
			orderCreate.put("DestLatitude", Long.parseLong(lat));

			orderCreate.put("Encrypt", 0);
			orderCreate.put("FareType", "001");
			array.add(orderCreate);
		}
		int ii=TempHttpUtil.sendPost_JSON(url, array, "orderCreate");
		log.info("第15条共推送{}条数据",pk);
		System.out.println("==========15======"+ii+"================");
		/**
		 * 16订单完成
		 * 17订单取消
		 * @author fire
		 */
		String finishUrl="/order/match";
		JSONArray finish=new JSONArray();
		String cancelUrl="/order/cancel";
		JSONArray cancel=new JSONArray();
		int aa=0;
		int bb=0;
		for(Order order:list2){
			if(order.getOrderStatus()==6){
				aa++;
				JSONObject orderMatch = new JSONObject();
				String companyId=Props.getValue("ecar.companyId");
				orderMatch.put("CompanyId", companyId);
				orderMatch.put("Address", 420107);
				orderMatch.put("OrderId", order.getOrderNum());
				orderMatch.put("Encrypt", 0);
				orderMatch.put("LicenseId", driverService.selectByPrimaryKey(order.getDriverId()).getLicenseId()==null?"11111":driverService.selectByPrimaryKey(order.getDriverId()).getLicenseId());
				orderMatch.put("DriverPhone", driverService.selectByPrimaryKey(order.getDriverId()).getPhone());
				orderMatch.put("VehicleNo", carMapper.selectByDriverId(order.getDriverId()).getNumberPlate());
				orderMatch.put("DistributeTime",Long.parseLong(DateUtil.parseDateToString(order.getReceiveTime(), DateUtil.DATE_FORMAT_COMPACTFULL)));
				finish.add(orderMatch);
			}else if(order.getOrderStatus()==1){
				bb++;
				JSONObject orderCancel = new JSONObject();
				String companyId=Props.getValue("ecar.companyId");
				orderCancel.put("CompanyId", companyId);
				orderCancel.put("Address", 420107);
				orderCancel.put("OrderId", order.getOrderNum());
				orderCancel.put("CancelTime", Long.parseLong(DateUtil.parseDateToString(order.getEndTime(), DateUtil.DATE_FORMAT_COMPACTFULL)));
				orderCancel.put("Operator", "1");
				orderCancel.put("CancelTypeCode", "1");
				cancel.add(orderCancel);
			}
		}
		i=TempHttpUtil.sendPost_JSON(finishUrl, finish, "orderMatch");
		log.info("第16条共推送{}条数据",aa);
		System.out.println("==========16======"+i+"================");
		i=TempHttpUtil.sendPost_JSON(cancelUrl, cancel, "orderCancel");
		log.info("第17条共推送{}条数据",bb);
		System.out.println("==========17======"+i+"================");


		/**
		 * 20经营出发
		 * 21经营到达
		 * 30合乘驾驶员行程发布
		 * @author txp
		 */
		String startUrl="/operate/depart";
		String overUrl="/operate/arrive";
		String pcUrl = "/share/route";
		JSONArray depart=new JSONArray();
		JSONArray arrive=new JSONArray();
		JSONArray shareRoute=new JSONArray();
		int a1=0;
		int a2=0;
		int a3=0;
		for(Order order:list2){
			if(order.getOrderStatus()==6){
				a1++;
				JSONObject operate = new JSONObject();
				JSONObject operatearrive = new JSONObject();
				JSONObject share = new JSONObject();
				String companyId=Props.getValue("ecar.companyId");
				operate.put("CompanyId", companyId);
				operate.put("OrderId", order.getOrderNum());
				operate.put("LicenseId", driverService.selectByPrimaryKey(order.getDriverId()).getLicenseId()==null?"11111111":driverService.selectByPrimaryKey(order.getDriverId()).getLicenseId());
				operate.put("FareType", "001");
				operate.put("VehicleNo", carMapper.selectByDriverId(order.getDriverId()).getNumberPlate());
				String[] s = order.getStartLocation().split(":");

				s[0]=s[0].concat("000000");
				s[1]=s[1].concat("000000");
				String lon=s[0].substring(0, 9);
				lon=lon.replace(".", "");
				String lat=s[1].substring(0, 10);
				lat=lat.replace(".", "");
				operate.put("DepLongitude", Long.parseLong(lon));
				operate.put("DepLatitude", Long.parseLong(lat));
				operate.put("Encrypt", 0);
				operate.put("DepTime",Long.parseLong(DateUtil.parseDateToString(order.getStartTime(), DateUtil.DATE_FORMAT_COMPACTFULL)));
				depart.add(operate);
				operatearrive.put("CompanyId", companyId);
				operatearrive.put("OrderId", order.getOrderNum());
				String[] ss = order.getDesLocation().split(":");
				ss[0]=ss[0].concat("000000");
				ss[1]=ss[1].concat("000000");
				lon=ss[0].substring(0, 9);
				lon=lon.replace(".", "");
				lat=ss[1].substring(0, 10);
				lat=lat.replace(".", "");
				operatearrive.put("DestLongitude", Long.parseLong(lon));
				operatearrive.put("DestLatitude", Long.parseLong(lat));
				operatearrive.put("Encrypt", 0);
				operatearrive.put("DestTime",Long.parseLong(DateUtil.parseDateToString(order.getEndTime(), DateUtil.DATE_FORMAT_COMPACTFULL)));
				OrderPriceDetail detail = orderPriceDetailMapper.selectByOrderNum(order.getOrderNum());
				operatearrive.put("DriveMile", detail.getRoadDistance());
				operatearrive.put("DriveTime", detail.getDriverTime()*60);
				a2++;
				arrive.add(operatearrive);


				if (5==order.getOrderType() || 7==order.getOrderType()) {
					a3++;
					Driver driver = driverService.selectByPrimaryKey(order.getDriverId());
					share.put("CompanyId", companyId);
					share.put("RouteId", order.getOrderNum());
					share.put("DriverName", driver.getName());
					share.put("DriverPhone", driver.getPhone());
					share.put("LicenseId", driver.getLicenseId()==null?"111111":driver.getLicenseId());
					share.put("VehicleNo", carMapper.selectByDriverId(driver.getId()).getNumberPlate());
					share.put("Departure", order.getStartAddress());
					String[] sss = order.getStartLocation().split(":");

					sss[0]=sss[0].concat("000000");
					sss[1]=sss[1].concat("000000");
					lon=sss[0].substring(0, 9);
					lon=lon.replace(".", "");
					lat=sss[1].substring(0, 10);
					lat=lat.replace(".", "");
					share.put("DepLongitude", Long.parseLong(lon));
					share.put("DepLatitude", Long.parseLong(lat));
					share.put("Destination", order.getDesAddress());
					String[] str= order.getDesLocation().split(":");

					str[0]=str[0].concat("000000");
					str[1]=str[1].concat("000000");

					lon=str[0].substring(0, 9);
					lon=lon.replace(".", "");
					lat=str[1].substring(0, 10);
					lat=lat.replace(".", "");
					share.put("DestLongitude", Long.parseLong(lon));
					share.put("DestLatitude", Long.parseLong(lat));
					share.put("Encrypt", 0);
					share.put("RouteCreateTime", Long.parseLong(DateUtil.parseDateToString(order.getCreateTime(),DateUtil.DATE_FORMAT_COMPACTFULL)));
					shareRoute.add(share);
				}
			}
		}
		i=TempHttpUtil.sendPost_JSON(startUrl, depart, "operateDepart");
		log.info("第20条共推送{}条数据",a1);
		System.out.println("==========20======"+i+"================");
		i=TempHttpUtil.sendPost_JSON(overUrl, arrive, "operateArrive");
		log.info("第21条共推送{}条数据",a2);
		System.out.println("==========21======"+i+"================");
		i=TempHttpUtil.sendPost_JSON(pcUrl, shareRoute, "shareRoute");
		log.info("第30条共推送{}条数据",a3);
		System.out.println("==========30======"+i+"================");


		baseInfoService.company();//1，成功 201
		baseInfoService.companystat();//2，成功 201
		baseInfoService.companypay();//3，成功 201
		baseInfoService.companyservice();//4，成功 201
		baseInfoService.companypermit();//5,成功 201
		baseInfoService.companyfare();//6,成功 201
		baseInfoService.companyvehicle();//7,成功 201
		baseInfoService.companyvehicleinsurance();//8,成功 201
		tempService.postBaseInfoVehicleTotalMile();//9,成功 201
		tempService.postBaseInfoDriver();//10,成功 201
		tempService.postBaseInfoDriverEducate();//11,成功 201
		tempService.postBaseInfoDriverApp();//12,成功 201
		tempService.postBaseInfoDriverStat();//13,成功 201
		baseInfoService.passenger();//14,成功 201
		//15+16+17
		//15,成功 201
		//16,成功 201
		//17,成功 201

		
		List<Driver> drivers=driverMapper.selectDriverByOrderModel(1);
		pk=0;
		for(Driver driver:drivers){
			pk++;
			
			tempService.postOperateLogin(driver);//18,成功 201
			tempService.postOperateLogOut(driver);//19,成功 201
			tempService.rateDriver(driver.getId());//28,成功 201
		}
		log.info("第18,19,28条共推送{}条数据",pk);
		//20+21
		//20成功 201
		//21成功 201

		baseInfoService.operatePayInfo();//22,成功 201
		baseInfoService.positionDriverInfo();//23,成功 201
		baseInfoService.positionVehicleInfo();//24,成功 201
		baseInfoService.passagengerEvaluateInfo();//25,成功 201
		baseInfoService.passengerComplaintInfo();//26,成功 201
		//27,已测成功 201
		baseInfoService.sharecompany();//29,成功 201
		//30已测成功 201



		baseInfoService.shareOrder();//31成功 201
		baseInfoService.sharepay();//32成功 201




	}
}





